package com.contextor.magiccards.services;

import com.contextor.magiccards.dao.GenericDao;
import com.contextor.magiccards.model.Entity;
import com.contextor.magiccards.model.errors.MagicCardsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * User: Sergey
 * Date: 14.03.2011 22:35:33
 */
@Component
@Transactional(readOnly = true)
public abstract class BaseService<T extends Entity, D extends GenericDao> implements BaseEntityService<T, D> {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected D dao;

    protected BaseService(D dao) {
        this.dao = dao;
    }

    @SuppressWarnings("unchecked")
    public T getById(int shardId, Number pk) {
        return (T) this.dao.getById(shardId, pk);
    }

    @SuppressWarnings("unchecked")
    public T exist(int shardId, Number pk) throws MagicCardsException {
        return (T) this.dao.exist(shardId, pk);
    }

    @SuppressWarnings("unchecked")
    public T loadById(int shardId, Number pk) {
        return (T) this.dao.loadById(shardId, pk);
    }

    @SuppressWarnings("unchecked")
    @Transactional(readOnly = false)
    public void save(int shardId, T bean) {
        this.dao.save(shardId, bean);
    }

    @SuppressWarnings("unchecked")
    @Transactional(readOnly = false)
    public void remove(int shardId, Number pk) throws Exception {
        this.dao.remove(shardId, pk);
    }

}
